<?php

/**
 * This is the model class for table "tbl_sanpham".
 *
 * The followings are the available columns in table 'tbl_sanpham':
 * @property integer $Id
 * @property string $TenSanPham
 * @property string $MoTa
 * @property string $DonGia
 * @property string $AnhDaiDien
 * @property integer $STT
 * @property integer $TrangThai
 */
class sanpham extends CActiveRecord
{
	public $path;
	public $checkCate;
	public $multiPath;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return sanpham the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_sanpham';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('TenSanPham, AnhDaiDien, STT, checkCate', 'required'),
			array('STT, TrangThai', 'numerical', 'integerOnly'=>true),
			array('TenSanPham', 'length', 'max'=>200),
			array('DonGia', 'length', 'max'=>20),
			array('AnhDaiDien', 'length', 'max'=>300),
			array('MoTa', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('Id, TenSanPham, MoTa, DonGia, AnhDaiDien, STT, TrangThai', 'safe', 'on'=>'search'),
			array('path', 'file', 'types'=>'jpg,jpeg,gif,png', 'maxSize'=>1024*1024*16,'allowEmpty'=>true),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'Id' => 'ID',
			'TenSanPham' => 'Ten San Pham',
			'MoTa' => 'Mo Ta',
			'DonGia' => 'Don Gia',
			'AnhDaiDien' => 'Anh Dai Dien',
			'STT' => 'Stt',
			'TrangThai' => 'Trang thai',
			'checkCate' => 'Danh mục SP',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('Id',$this->Id);
		$criteria->compare('TenSanPham',$this->TenSanPham,true);
		$criteria->compare('MoTa',$this->MoTa,true);
		$criteria->compare('DonGia',$this->DonGia,true);
		$criteria->compare('AnhDaiDien',$this->AnhDaiDien,true);
		$criteria->compare('STT',$this->STT);
		$criteria->compare('TrangThai',$this->TrangThai);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function LaySanPhamTheoTrangThai($TrangThai, $Limit = 0) {
		if (is_numeric($TrangThai) && is_numeric($Limit)) {
			$sql = "SELECT * FROM tbl_sanpham WHERE TrangThai = '".$TrangThai."' ORDER BY STT,Id ASC";
			$sql .= ($Limit > 0) ? " LIMIT 0, " . $Limit : "";
			$data = $this->findAllBySql($sql);
			return isset($data) ? $data : false;
		} else 
			return false;
	}
	
	public function LaySanPhamTheoId($id) {
		if (is_numeric($id)) {
			$sql = "SELECT * FROM tbl_sanpham WHERE TrangThai != 0 AND Id = '".$id."'";
			$data = $this->findBySql($sql);
			return isset($data) ? $data : false;
		} else 
			return false;
	}
}